package com.yoomiito.app.ui.my.income

import android.os.Bundle
import cn.droidlover.xdroidmvp.net.ApiException
import com.yoomiito.app.Constant
import com.yoomiito.app.adapter.my.MonthOrderAdapter
import com.yoomiito.app.model.my.MyMonthOrderInfo
import com.yoomiito.app.ui.fragment.my.MyOrderBaseFragment
import com.yoomiito.app.utils.Const

/**
 * 显示跟收入有关的订单
 */
class RevenueRelatedOrdersFragment : MyOrderBaseFragment<RevenueRelatedOrdersFmPresenter>() {

    private var mAdapter: MonthOrderAdapter? = null
    private var month = 0
    private var mPageType = IncomeForecastActivity.TAOBAO
    override fun newP(): RevenueRelatedOrdersFmPresenter = RevenueRelatedOrdersFmPresenter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val arguments = arguments
        if (arguments != null) {
            month = arguments.getInt(Constant.KEY_MONTH, 0)
            mPageType = arguments.getInt(Const.Common.Type, IncomeForecastActivity.TAOBAO)
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mAdapter = MonthOrderAdapter()
        mRecyclerView.adapter = mAdapter
        showLoading()
        getNewData()
    }

    override fun getNewData() {
        when (mPageType) {
            IncomeForecastActivity.TAOBAO -> p.getData(currentPage, month)
            IncomeForecastActivity.JINGDONG -> p.getJDData(currentPage)
            IncomeForecastActivity.CreditCard -> {
                p.getCreditCard(currentPage)
            }
        }
    }

    override fun getEmptyContent(): String = when (mPageType) {
        IncomeForecastActivity.CreditCard -> ""
        else -> "未购买商品"
    }


    override fun handleError(e: ApiException) {}

    fun setMonthOrderData(data: MyMonthOrderInfo?) {
        dismissLoading()
        (context as IncomeForecastActivity).moneyTv.text = data?.monthIncome
        if (currentPage == 1) {
            if (data == null || data.data == null || data.data.size == 0) {
                mStatusManager.showEmpty()
            } else {
                mAdapter?.setNewData(data.data)
                mSmartRefreshLayout.finishRefresh(true)
                mStatusManager.showContent()
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true)
            if (data?.data == null || data?.data.size == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                mAdapter?.addData(data?.data)
            }
        }
    }

    fun finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true)
        mSmartRefreshLayout.finishLoadMore(true)
    }
}